package cn.smile.controller.home;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smile.bean.CodeMsg;
import cn.smile.bean.PageBean;
import cn.smile.bean.Result;
import cn.smile.constant.SessionConstant;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.News;
import cn.smile.entity.common.Student;
import cn.smile.service.common.GoodsService;
import cn.smile.service.common.NewsService;
import cn.smile.service.common.StudentService;
import cn.smile.util.SessionUtil;
import cn.smile.util.ValidateEntityUtil;

import javax.annotation.Resource;

/**
 * 前台首页控制器
 *
 * @author Administrator
 */
@RequestMapping("/home/index")
@Controller
public class IndexController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private StudentService studentService;
    @Resource
    private NewsService newsService;

    /**
     * 前台首页
     */
    @RequestMapping(value = "/index")
    public String index(Model model, PageBean<Goods> pageBean, Goods goods) {
        pageBean.setPageSize(12);
        goods.setStatus(Goods.GOODS_STATUS_UP);
        model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
        model.addAttribute("name", goods.getName());
        model.addAttribute("newsList", newsService.findList(3));
        return "home/index/index";
    }

    /**
     * 新闻详情页面
     */
    @RequestMapping(value = "/news_detail")
    public String index(Model model, @RequestParam(name = "id") Long id) {
        News news = newsService.find(id);
        model.addAttribute("news", news);
        news.setViewNumber(news.getViewNumber() + 1);
        newsService.save(news);
        return "home/index/news_detail";
    }

    /**
     * 用户登录页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "home/index/login";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        SessionUtil.set(SessionConstant.SESSION_STUDENT_LOGIN_KEY, null);
        return "redirect:login";
    }

    /**
     * 检查学号是否存在
     */
    @RequestMapping(value = "/check_sn", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> checkSn(@RequestParam(name = "sn") String sn) {
        Student student = studentService.findBySn(sn);
        return Result.success(student == null);
    }

    /**
     * 用户注册表单提交
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> register(Student student) {
        CodeMsg validate = ValidateEntityUtil.validate(student);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        //基本验证通过
        if (studentService.findBySn(student.getSn()) != null) {
            return Result.error(CodeMsg.HOME_STUDENT_REGISTER_SN_EXIST);
        }
        student = studentService.save(student);
        if (student == null) {
            return Result.error(CodeMsg.HOME_STUDENT_REGISTER_ERROR);
        }
        //表示注册成功，此时将用户信息放入session
        SessionUtil.set(SessionConstant.SESSION_STUDENT_LOGIN_KEY, student);
        return Result.success(true);
    }

    /**
     * 用户登录表单提交
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> login(@RequestParam(name = "sn") String sn,
                                 @RequestParam(name = "password") String password) {
        Student student = studentService.findBySn(sn);
        if (student == null) {
            return Result.error(CodeMsg.HOME_STUDENT_REGISTER_SN_EXIST);
        }
        student = studentService.save(student);
        if (student == null) {
            return Result.error(CodeMsg.HOME_STUDENT_SN_NO_EXIST);
        }
        //表示学号存在，验证密码是否正确
        if (!student.getPassword().equals(password)) {
            return Result.error(CodeMsg.HOME_STUDENT_PASSWORD_ERROR);
        }
        //验证用户状态是否被冻结
        if (student.getStatus() != Student.STUDENT_STATUS_ENABLE) {
            return Result.error(CodeMsg.HOME_STUDENT_UNABLE);
        }
        //表示一切都符合，此时将用户信息放入session
        SessionUtil.set(SessionConstant.SESSION_STUDENT_LOGIN_KEY, student);
        return Result.success(true);
    }
}
